/*
 * @Author: cmd
 * @we_chat: cmd_develop
 * @Date: 2018-12-24 22:01:15
 * @LastEditors: cmd
 * @LastEditTime: 2019-03-20 22:45:22
 * @Description: 组件管理器
 * App.component_manager.show_alert_tips("是兄弟，就来砍我~~~~~~~");
 */

const {ccclass, property} = cc._decorator;

@ccclass
export default class component_manager extends cc.Component {

    // onLoad () {}

    start () {

    }

    _alert_tips = false;
    _alert_loading = false;

    loadPrefabNode(path, cb) {
        console.log("loadPrefabNode 加载资源");
        cc.loader.loadRes("prefabs/" + path, function (err, res) {
            if(err) {
                console.error("load_prefab err:",err);
                return;
            }
            let node = cc.instantiate(res);
            if (node == undefined) {
                console.error('loadPrefab undefined path = '+path);
            } else {
                cb && cb(node);
            }
        });
        console.log("loadPrefabNode 加载完成");
    }

    show_alert_tips(text:string){
        // let win_path:string = 'component/alert_tips';
        // let temp_node = cc.find("Canvas").getChildByName('alert_tips');       
        // if (temp_node === null) { 
        //     this.loadPrefabNode(win_path, function (temp_node) {
        //         console.log('加载窗口');                 
        //         cc.find("Canvas").addChild(temp_node,100,'alert_tips'); 
        //         let script = temp_node.getComponent("component_alert");
        //         script.show_tips(text);    
        //     }.bind(this));
        // } 
        // else {   
        //     let script = temp_node.getComponent("component_alert");
        //     script.show_tips(text);   
        // }
        var btn_temp = this._alert_tips;
        let temp_node = cc.find("Canvas").getChildByName('alert_tips');        
        if (temp_node != undefined) { 
            let script = temp_node.getComponent("component_alert");
            script.show_tips(text); 
        } 
        else {            
            if (btn_temp) {
                return;
            }            
            btn_temp = true;  
            this.loadPrefabNode('common/alert_tips', function (temp_node) {
                console.log('加载alert_tips');                 
                cc.find("Canvas").addChild(temp_node, 101, 'alert_tips');  
                let script = temp_node.getComponent("component_alert");
                script.show_tips(text);                      
                btn_temp = false;
            }.bind(this));
        }
    }

    show_alert_loading(text:string){
        var btn_temp = this._alert_tips;
        let temp_node = cc.find("Canvas").getChildByName('alert_loading');        
        if (temp_node != undefined) { 
            let script = temp_node.getComponent("component_alert");
            script.show_loading(text); 
        } 
        else {            
            if (btn_temp) {
                return;
            }            
            btn_temp = true;  
            this.loadPrefabNode('component/alert_loading', function (temp_node) {
                console.log('加载alert_loading');                 
                cc.find("Canvas").addChild(temp_node, 100, 'alert_loading');  
                let script = temp_node.getComponent("component_alert");
                script.show_loading(text);                      
                btn_temp = false;
            }.bind(this));
        }
    }

    close_alert_loading(){
        var btn_temp = this._alert_tips;
        let temp_node = cc.find("Canvas").getChildByName('alert_loading');        
        if (temp_node != undefined) { 
            let script = temp_node.getComponent("component_alert");
            script.close_loading(); 
        } 
        else {            
            if (btn_temp) {
                return;
            }            
            btn_temp = true;  
            this.loadPrefabNode('component/alert_loading', function (temp_node) {
                console.log('加载alert_loading');                 
                cc.find("Canvas").addChild(temp_node, 100, 'alert_loading');  
                let script = temp_node.getComponent("component_alert");
                script.close_loading();                      
                btn_temp = false;
            }.bind(this));
        }
    }

    

    // update (dt) {}
}
